﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fortune.POCO.DO;

namespace Fortune.Util
{
    public static class ProtoUtil
    {
        public static List<StringDO> toDataObject(IEnumerable<StringProto> stringProtos)
        {
            return stringProtos.Select(of).ToList();
        }

        public static List<ItemDO> toDataObject(IEnumerable<ItemProto> itemProtos)
        {
            return itemProtos.Select(of).ToList();
        }
        
        public static StringDO of(StringProto stringProto)
        {
            return new StringDO
            {
                protoId = stringProto.ID,
                name = stringProto.Name,
                sid = stringProto.SID,
                zhCN = stringProto.ZHCN,
                enUS = stringProto.ENUS,
                frFR = stringProto.FRFR,
                gameVersion = GameConfig.gameVersion.ToFullString()
            };
        }
        
        public static ItemDO of(ItemProto itemProto)
        {
            return new ItemDO
            {
                type = (int) itemProto.Type,
                protoId = itemProto.ID,
                name = itemProto.Name,
                sid = itemProto.SID,
                gameVersion = GameConfig.gameVersion.ToFullString(),
                miningFrom = itemProto.miningFrom,
                produceForm = itemProto.produceFrom,
                stackSize = itemProto.StackSize,
                isFluid = itemProto.IsFluid,
                isEntity = itemProto.IsEntity,
                canBuild = itemProto.CanBuild,
                buildInGas = itemProto.BuildInGas,
                iconPath = itemProto.IconPath,
                modelIndex = itemProto.ModelIndex,
                modelCount = itemProto.ModelCount,
                hpMax = itemProto.HpMax,
                ability = itemProto.Ability,
                heatValue = itemProto.HeatValue,
                potential = itemProto.Potential,
                reactorInc = itemProto.ReactorInc,
                fuelType = itemProto.FuelType,
                buildIndex = itemProto.BuildIndex,
                buildMode = itemProto.BuildMode,
                gridIndex = itemProto.GridIndex,
                unlockKey = itemProto.UnlockKey,
                preTechOverride = itemProto.PreTechOverride,
                descFields = parseIntArray(itemProto.DescFields),
                description = itemProto.Description,
                isRaw = itemProto.isRaw,
                handcraftProductCount = itemProto.handcraftProductCount,
                maincraftProductCount = itemProto.maincraftProductCount,
                missingTech = itemProto.missingTech
            };
        }

        private static string parseIntArray(IReadOnlyList<int> array)
        {
            switch (array.Count)
            {
                case 0:
                    return "";
                case 1:
                    return "" + array[0];
                default:
                    StringBuilder builder = new StringBuilder();
                    builder.Append(array[0]);
                    for (int i = 1; i < array.Count; i++)
                    {
                        builder.Append(",").Append(i);
                    }

                    return builder.ToString();
            }
        }
    }
}